/**
 * Created by kangli on 2018/4/25.
 */
var sum;
var counts='';
var pageThis =0;
var pagecount = 10;
var index = 0;
var wheretext = '';
var schoolID=''
$(function(){
    //得到学校列表
    getschoolLists();
    $(".searchNameInput").keypress(function(e) {
        // 回车键事件
        if(e.which == 13) {
            e.preventDefault();
            getschoolLists();
        }
    });

    $("#chooseImg").change(function(){
        var files = this.files;
       /* console.log(files.length)*/
        $.each(files,function(i,e){
            var fileReader = new FileReader();
            fileReader.readAsDataURL(e);
            e.mark = Math.random();
            fileReader.onload = function(){
                $("#logoImg").attr("src",this.result);
                $(".sd-examplePic").css("display","none");
                /*subImg.find("#hrefimg").attr("href",this.result);*/
            }
        })
    })

    $("#chooseImg1").change(function(){
        var files = this.files;
        /*console.log(files.length)*/
        $.each(files,function(i,e){
            var fileReader = new FileReader();
            fileReader.readAsDataURL(e);
            e.mark = Math.random();
            fileReader.onload = function(){
                $("#logoImg1").attr("src",this.result);
            }
        })
    })

})
function getschoolLists(){
    wheretext=$.trim($(".searchNameInput").val());
    var  data={page: index ,size: pagecount,schoolName:wheretext};
    AjaxSubmit("GET",juge(data), basePath + "/super/common/school/getSchool",getSchool_fun);
}
function getSchool_fun(res){
    console.log(res);
    if(res.status=='SUCCESS'){
        var schoolLists = res.obj.content;
        sum = res.obj.totalElements;
        counts = sum / pagecount;
        if (schoolLists.length != 0) {
            $("#userPage").css("display","block");
            if (sum % pagecount == 0) {
                counts = parseInt(sum / pagecount);
            }
            else {
                counts = parseInt(sum / pagecount) + 1;
            }
            $("#userPage").paging({
                pageNo: pageThis,
                totalPage: counts,
                totalSize: sum,
                callback: function (num) {
                    index = num - 1;
                    getschoolLists();
                    pageThis = num;
                }
            });
            $(".userTbody").empty();
            $('.warnDiv').remove();
            $.each(schoolLists, function (i, schoolList) {
                var type = '';
                var imgstr='';
                var schoolName = schoolList.schoolName || '';
                var teacherName = schoolList.teacherName || '';
                if(schoolList.buildType=='PRIVATE_BUILD') {
                    type='私立'
                }else if(schoolList.buildType=='STATE_BUILD') {
                    type='公立'
                }

                if(schoolList.pic!=null&&schoolList.pic!=''&&schoolList.pic!=undefined){
                    imgstr='<img src="'+schoolList.pic+'">'
                }else{
                    imgstr='<img src="'+schoolList.pic+'">'
                }
                var brief = schoolList.brief || '';
                var html = '<tr> ' +
                    '<td class="texC">'+schoolName+'</td> ' +
                    '<td class="texC">'+imgstr+'</td> ' +
                    '<td class="texC">'+brief+'</td> ' +
                    '<td class="texC"> '+type+'</td> ' +
                    '<td class="texC"><span class=" cursorP  colorBlue"  data-id="'+schoolList.id+'"  data-target="#editUser" data-toggle="modal" onclick="editSchool(this)">编辑</span></td> ' +
                    '</tr>';
                $(".userTbody").append(html)
            })

        }
        else{
            $("#userPage").css("display","none");
            $(".userTbody").empty();
            $('.warnDiv').remove();
            var html = $('<div class="text-center warnDiv "  style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
            $(".userTable").after(html)
        }
    }
}
//编辑学校
function editSchool(btn){
    schoolID = $(btn).attr("data-id");
    var data = '{"schoolId":"' + schoolID + '"}';
    AjaxSubmit("GET",JSON.parse(data),basePath + "/super/common/school/getSchoolById",getSchoolById_fun);
    function getSchoolById_fun(res){
        console.log(res);
        var pic = res.obj.pic || '';
        $(".sd-name1").val(res.obj.schoolName);
        var brief = res.obj.brief || '';
        $(".brief1").val(brief);
        $("#logoImg1").attr("src",pic);

        if(res.obj.buildType=='PRIVATE_BUILD') {
            $(".sd-Type1").children("option").eq(1).attr("selected","selected");
         /*   type='私立'*/
        }else if(res.obj.buildType=='STATE_BUILD') {
            $(".sd-Type1").children("option").eq(0).attr("selected","selected");
          /*  type='公立'*/
        }
    }

}
/*保存编辑*/
function saveSchool(){
    var name = $(".sd-name1").val();
    var brief = $(".brief1").val();
    var type1=$(".sd-Type1").children("option:selected").text();
    var truetype='';
    if(type1=='公立'){
        truetype='STATE_BUILD'
    }else if(type1=='私立'){
        truetype='PRIVATE_BUILD'
    }
    var formData1 = new FormData();
    if($("#chooseImg1").val()!=""&&$("#chooseImg1").val()!=null&&$("#chooseImg1").val()!=undefined) {
        formData1.append("file", $("#chooseImg1")[0].files[0]);
    }
    formData1.append("id",schoolID);
    formData1.append("schoolName",name);
    formData1.append("brief", brief);
    formData1.append("buildType",truetype);
    if(name!=''&&name!=undefined&&name!=undefined){
            $.ajax({
                type : 'POST',
                url : basePath+"/super/common/school/updateSchool",
                data : formData1,
                processData : false,
                contentType : false,
                success : function(result) {
                    console.log(result)
                    index=$("#userPage").find(".active").find("a").text()-1;
                    getschoolLists();
                },
                error : function(err) {
                }
            });
            $("#editUser").modal("hide");
    }else{
        alert("请输入学校名称!")
    }


}
/*新增学校*/
function addschool(btn){
    var name = $(".sd-name").val();
    var brief = $(".brief").val();
    var type=$(".sd-Type").children("option:selected").text();
    var truetype='';
    if(type=='公立'){
        truetype='STATE_BUILD'
    }else if(type=='私立'){
        truetype='PRIVATE_BUILD'
    }
    if(name!=''&&name!=undefined&&name!=undefined) {
        if ($("#chooseImg").val() != "" && $("#chooseImg").val() != null && $("#chooseImg").val() != undefined) {
            var formData = new FormData();
            formData.append("file", $("#chooseImg")[0].files[0]);
            formData.append("schoolName", name);
            formData.append("brief", brief);
            formData.append("buildType",truetype);

            $.ajax({
                type : 'POST',
                url : basePath+"/super/common/school/addSchool",
                data : formData,
                processData : false,
                contentType : false,
                success : function(result) {
                    /*console.log(result)*/
                    alert("添加成功");
                    index=$("#userPage").find(".active").find("a").text()-1;
                    getschoolLists();
                },
                error : function(err) {
                }
            });

            $("#addUser").modal("hide");
        }else{
            alert("请先选择logo")
        }
    }else{
        alert("请先输入学校名称")
    }

}


//判断字符不空就存
function juge(obj){
    var result={};
    for(i in obj){
        if(obj[i]==null||obj[i]==""){
            continue;
        }
        var value=obj[i];
        eval("result."+i+"='"+value+"'");
    }
    return result;
}